package com.carbon.mq.feishu;

import cn.hutool.json.JSONUtil;
import com.carbon.common.constants.AccountConstant;
import com.carbon.common.feishu.FeiShuAPI;
import com.carbon.domain.common.constant.RocketMqName;
import com.carbon.domain.mq.entity.OpenRegisterAccount;
import com.carbon.mq.entity.SysAccount;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

/**
 * [一句话描述该类的功能]
 *
 * @author : [Lenovo]
 * @version : [v1.0]
 * @createTime : [2024/8/26 下午2:46]
 */
@Slf4j
@Component
@RocketMQMessageListener(consumerGroup = RocketMqName.CA_MSG, topic = RocketMqName.CA_MSG)
public class UpdateRegisterAccountReceiver implements RocketMQListener<MessageExt> {
    @Override
    public void onMessage(MessageExt message) {
        log.info("msg id: {}, payload: {}", message.getMsgId(), new String(message.getBody()));
        SysAccount data = JSONUtil.toBean(new String(message.getBody()), SysAccount.class);
        OpenRegisterAccount openRegisterAccount = new OpenRegisterAccount();
        openRegisterAccount.setContactNumber(data.getPhone());
        openRegisterAccount.setAccountName(data.getAccountName());
        openRegisterAccount.setContactEmail(data.getEmail());
        openRegisterAccount.setProductVersion(data.getProductVersion());
        if (data.getAccountStatus().equals(AccountConstant.ACCOUNT_STATUS_DISABLE))
        {
            openRegisterAccount.setAccountState("已销户");
        }else if (data.getAccountStatus().equals(AccountConstant.ACCOUNT_STATUS_ENABLE)){
            openRegisterAccount.setAccountState("已开户");
        }else {
            openRegisterAccount.setAccountState("未开户");
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = data.getCreatedTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        String formattedDate = localDateTime.format(formatter);
        openRegisterAccount.setOpenAccountTime(formattedDate);
        FeiShuAPI.updateForm(FeiShuAPI.spreadsheetToken,"58ad94",openRegisterAccount.getAccountName(),openRegisterAccount,"N");
    }
}
